// GaojieDrive.cpp : Defines the entry point for the application.
//

#include "pch.h"
#include "GaojieDriveApp.h"

namespace
{

std::unique_ptr<CGaojieDriveApp> g_pTheApp;

void RunApp(HINSTANCE hInstance, LPCWSTR lpCmdLine)
{
#if 0
	if (AllocConsole())
	{
		FILE *pFile;
		_tfreopen_s(&pFile, _T("CONOUT$"), _T("w+t"), stdout);
		_tsetlocale(LC_CTYPE, _T(""));
	}
#endif

	INITCOMMONCONTROLSEX iccx = { sizeof(iccx) };
	iccx.dwICC = ICC_LISTVIEW_CLASSES | ICC_LINK_CLASS;
	InitCommonControlsEx(&iccx);

	SetInUIThread(true);
	LogInit();
	g_pTheApp = std::make_unique<CGaojieDriveApp>(hInstance);

	if (g_pTheApp->Init(lpCmdLine))
	{
		MSG msg;
		while (GetMessage(&msg, NULL, 0, 0))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	else
		LogError(L"App Init error");

	g_pTheApp->Exit();
	g_pTheApp = nullptr;
	LogExit();
}

}	// namespace

const std::unique_ptr<CGaojieDriveApp>& GetApp()
{
	return g_pTheApp;
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
	_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);

	if (HANDLE hMutex = CreateMutexW(NULL, FALSE, L"GaojieDriveMutex"))
	{
		if (GetLastError() == 0)
			RunApp(hInstance, lpCmdLine);

		CloseHandle(hMutex);
	}

	return 0;
}
